<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 10:04:26
 * @LastEditTime: 2020-12-28 10:08:31
 * @LastEditors: Please set LastEditors
 * @Description: 防火墙规则
 * @FilePath: /special/application/adminapiv3/controller/module_manage/netfilter/FirewallRuleController.php
 */
namespace app\adminapiv3\controller\module_manage\netfilter;

use app\adminapiv3\service\module_manage\netfilter\FirewallRuleService;
use think\facade\Request;

class FirewallRuleController extends NetfilterBaseController
{
    /**
     * @name: 防火墙规则列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 10:30:26
     */
    public function firewallRuleList (\think\Request $request)
    {
        $input = $request->only([
            'src_ip','dest_ip','dest_port',
            'src_port','have_mac_addr','status',
            'process','protocols','direction',
            'alias','host_id','type'
        ], 'post');
        $this->validate($input, 'FirewallRule.listFilter');
        
        $FirewallRuleService = new FirewallRuleService();
        $data = $FirewallRuleService->firewallRuleList($input);

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 添加手动规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 13:52:13
     */
    public function firewallRuleAdd (\think\Request $request)
    {
        $input = $request->only([
            'host_id','type','src_ip',
            'dest_ip','dest_port','src_port',
            'process','protocols','direction',
            'alias','force','learn_id','scope'=>'any'
        ], 'post');
        $input['have_mac_addr'] = 0;

        $this->validate($input, 'FirewallRule.add');

        $FirewallRuleService = new FirewallRuleService();
        $data = $FirewallRuleService->firewallRuleAdd($input);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut($data, true);
    }

    /**
     * @name: 防火墙规则修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 17:18:57
     */
    public function firewallRuleModify (\think\Request $request)
    {
        $input = $request->only([
            'id','fid','learn_id',
            'mac_addr','have_mac_addr','src_ip',
            'dest_ip','dest_port','src_port',
            'process','protocols','direction',
            'alias','force','host_id',
            'type','scope'=>'any'
        ], 'post');
        $this->validate($input, 'FirewallRule.modify');
        $FirewallRuleService = new FirewallRuleService();
        $data = $FirewallRuleService->firewallRuleModify($input);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut($data, true);
    }

    /**
     * @name: 防火墙规则删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-05 17:18:57
     */
    public function firewallRuleDelete ()
    {
        $input = Request::only(['fid','host_id'], 'post');

        $FirewallRuleService = new FirewallRuleService();
        $FirewallRuleService->firewallRuleDelete($input['host_id'], $input['fid']);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut([],true);
    }

    /**
     * @name: 启用规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 13:52:58
     */
    public function firewallRuleOn ()
    {
        $input = Request::only(['fid','host_id'],'post');
        $this->validate($input, 'FirewallRule.onOff');
        $FirewallRuleService = new FirewallRuleService();
        $FirewallRuleService->firewallRuleOn($input['host_id'],$input['fid']);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut([],true);
    }

    /**
     * @name: 关闭规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 13:52:58
     */
    public function firewallRuleOff ()
    {
        $input = Request::only(['fid','host_id'],'post');
        $this->validate($input, 'FirewallRule.onOff');
        $FirewallRuleService = new FirewallRuleService();
        $FirewallRuleService->firewallRuleOff($input['host_id'],$input['fid']);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut([],true);
    }
}